﻿//-------------------------------------
// WBFSSync - WBFSSync.exe
//
// Copyright 2009 Caian (ÔmΣga Frøst) <frost.omega@hotmail.com> :
//
// WBFSSync is Licensed under the terms of the Microsoft Reciprocal License (Ms-RL)
//
// ConfigurationPanel.cs:
//
// Painel de configurações do programa
//
//-------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WBFSSync.AeroControls
{
    public partial class ConfigurationPanel : UserControl
    {
        bool languageInitialized = false;
        FormLoderHomebrewEdit editDialog = new FormLoderHomebrewEdit();

        public ConfigurationPanel()
        {
            InitializeComponent();
            Visible = false;
        }

        public void SetLanguage()
        {
            SuspendLayout();

            if (!languageInitialized && Language.LanguageReady)
            {
                languageInitialized = true;

                tabPage_wbfssync.Text = Language.AppName;
                tabPage_covers.Text = Language.field_covers;
                tabPage_explorer.Text = Language.field_explorer;
                tabPage_loaders.Text = Language.field_loaders;

                label_log.Text = Language.field_log;
                label_noformat.Text = Language.field_no_format;
                label_ignore.Text = Language.field_ignore;
                label_update.Text = Language.field_update;
                label_language.Text = Language.field_language;
                label_skin.Text = Language.field_skin;
                label_need_restart.Text = Language.field_needrestart;

                checkBox_auto_download_covers.Text = Language.field_autodownload_covers;
                checkBox_covers_compatible_path.Text = Language.field_export_to_compatible_path;
                checkBox_covers_download_3d.Text = Language.field_3d_covers;
                checkBox_explorer_click.Text = Language.field_explorer_click;
                checkBox_format_override.Text = Language.field_close_format_window;
                checkBox_log_save_exception_log.Text = Language.field_save_exception_log;
                checkBox_log_save_log.Text = Language.field_save_log;
                checkBox_update_notify_only.Text = Language.field_notify_only;
                checkBox_operations_export_scrubbed.Text = Language.field_explorer_extract_scrubbed;
                checkBox_operations_autoscrubupdate.Text = Language.field_explorer_update_scrub;

                button_apply.Text = Language.field_apply;
                button_cancel.Text = Language.field_cancel;
                button_loader_edit.Text = Language.field_edit;

                comboBox_update_mode.Items.Clear();
                comboBox_update_mode.Items.Add(Language.menuitem_update_off);
                comboBox_update_mode.Items.Add(Language.menuitem_update_onexecute);
                comboBox_update_mode.Items.Add(Language.menuitem_update_onceaday);
                comboBox_update_mode.Items.Add(Language.menuitem_update_onceaweek);

                comboBox_language.Items.Clear();
                foreach (String s in Language.AvaliableLanguages)
                {
                    comboBox_language.Items.Add(s);
                }
                comboBox_language.Text = Language.SelectedLanguage;

                comboBox_skin.Items.Clear();
                foreach (String s in Skin.AvaliableSkins)
                {
                    comboBox_skin.Items.Add(s);
                }
                comboBox_skin.Text = Skin.SelectedSkin;

                columnHeader_name.Text = Language.field_name;
                columnHeader_path.Text = Language.field_path;
                columnHeader_width.Text = Language.field_width;
                columnHeader_height.Text = Language.field_height;

                editDialog.SetupLanguage();
            }

            ResumeLayout();
        }

        public void SetControls()
        {
            checkBox_auto_download_covers.Checked = Covers.AutoDownloadCovers;
            checkBox_covers_compatible_path.Checked = Covers.LookForCompatiblePath;
            checkBox_covers_download_3d.Checked = Covers.Download3DCoversCfg;

            checkBox_explorer_click.Checked = FormWBFSSync.EnablePathChangeSound;

            checkBox_format_override.Checked = DeviceManager.SupressFormatDriveWindow;

            checkBox_log_save_exception_log.Checked = Log.SaveExceptionLog;
            checkBox_log_save_log.Checked = Log.SaveLog;

            checkBox_update_notify_only.Checked = ProgramUpdate.NotifyOnly;
            comboBox_update_mode.SelectedIndex = (int)ProgramUpdate.UpdateMode;

            checkBox_operations_export_scrubbed.Checked = OperationManager.ExtractDiscScrubbed;
            checkBox_operations_autoscrubupdate.Checked = ScrubDisc.AutoUpdateScrub;

            textBox_deviceman_noformat.Text = DeviceManager.FormatLockedDrives;
            textBox_deviceman_ignore.Text = DeviceManager.IgnoredDrives;

            listView_cover_loader_homebrews.Items.Clear();
            foreach (LoaderHomebrew l in Covers.Loaders)
            {
                ListViewItem item = new ListViewItem(l.Name);
                item.SubItems.Add(l.Path);
                item.SubItems.Add(l.CoverWidth.ToString());
                item.SubItems.Add(l.CoverHeight.ToString());

                listView_cover_loader_homebrews.Items.Add(item);
            }
        }

        private void listView_cover_loader_homebrews_SelectedIndexChanged(object sender, EventArgs e)
        {
            button_loader_remove.Enabled = button_loader_edit.Enabled =
                (listView_cover_loader_homebrews.SelectedIndices.Count != 0);
        }

        DialogResult ShowLoaderEditDialog(bool newItem)
        {
            if (newItem)
            {
                editDialog.textBox_name.Text = "";
                editDialog.textBox_path.Text = "";

                editDialog.numericUpDown_width.Value = Covers.DefaultWidth;
                editDialog.numericUpDown_height.Value = Covers.DefaultHeight;
            }
            else
            {
                editDialog.textBox_name.Text = listView_cover_loader_homebrews.SelectedItems[0].SubItems[0].Text;
                editDialog.textBox_path.Text = listView_cover_loader_homebrews.SelectedItems[0].SubItems[1].Text;

                editDialog.numericUpDown_width.Value = Int32.Parse(listView_cover_loader_homebrews.SelectedItems[0].SubItems[2].Text);
                editDialog.numericUpDown_height.Value = Int32.Parse(listView_cover_loader_homebrews.SelectedItems[0].SubItems[3].Text);
            }

            return editDialog.ShowDialog(this);
        }

        private void button_loader_edit_Click(object sender, EventArgs e)
        {
            if (ShowLoaderEditDialog(false) == DialogResult.OK)
            {
                listView_cover_loader_homebrews.SelectedItems[0].SubItems[0].Text = editDialog.textBox_name.Text;
                listView_cover_loader_homebrews.SelectedItems[0].SubItems[1].Text = editDialog.textBox_path.Text;
                listView_cover_loader_homebrews.SelectedItems[0].SubItems[2].Text = editDialog.numericUpDown_width.Value.ToString();
                listView_cover_loader_homebrews.SelectedItems[0].SubItems[3].Text = editDialog.numericUpDown_height.Value.ToString();
            }
        }

        private void button_loader_remove_Click(object sender, EventArgs e)
        {
            listView_cover_loader_homebrews.SelectedItems[0].Remove();
        }

        private void button_loader_add_Click(object sender, EventArgs e)
        {
            if (ShowLoaderEditDialog(true) == DialogResult.OK)
            {
                ListViewItem item = new ListViewItem(editDialog.textBox_name.Text);
                item.SubItems.Add(editDialog.textBox_path.Text);
                item.SubItems.Add(editDialog.numericUpDown_width.Value.ToString());
                item.SubItems.Add(editDialog.numericUpDown_height.Value.ToString());

                listView_cover_loader_homebrews.Items.Add(item);
            }
        }

        public void Apply()
        {
            button_apply_Click(null, null);
        }

        private void button_apply_Click(object sender, EventArgs e)
        {
            Covers.AutoDownloadCovers = checkBox_auto_download_covers.Checked;
            Covers.LookForCompatiblePath = checkBox_covers_compatible_path.Checked;
            Covers.Download3DCoversCfg = checkBox_covers_download_3d.Checked;

            FormWBFSSync.EnablePathChangeSound = checkBox_explorer_click.Checked;

            DeviceManager.SupressFormatDriveWindow = checkBox_format_override.Checked;

            Log.SaveExceptionLog = checkBox_log_save_exception_log.Checked;
            Log.SaveLog = checkBox_log_save_log.Checked;

            ProgramUpdate.NotifyOnly = checkBox_update_notify_only.Checked;
            ProgramUpdate.UpdateMode = (UpdateMode)comboBox_update_mode.SelectedIndex;

            DeviceManager.FormatLockedDrives = textBox_deviceman_noformat.Text.ToUpper();
            DeviceManager.IgnoredDrives = textBox_deviceman_ignore.Text.ToUpper();

            Language.SelectedLanguage = Language.AvaliableLanguages[comboBox_language.SelectedIndex];
            Skin.SelectedSkin = Skin.AvaliableSkins[comboBox_skin.SelectedIndex];

            OperationManager.ExtractDiscScrubbed = checkBox_operations_export_scrubbed.Checked;

            Covers.Loaders.Clear();
            foreach (ListViewItem i in listView_cover_loader_homebrews.Items)
            {
                LoaderHomebrew l = new LoaderHomebrew();
                l.Name = i.SubItems[0].Text;
                l.Path = i.SubItems[1].Text;
                l.CoverWidth = Int32.Parse(i.SubItems[2].Text);
                l.CoverHeight = Int32.Parse(i.SubItems[3].Text);

                Covers.Loaders.Add(l);
            }

            Visible = false;
        }

        private void button_cancel_Click(object sender, EventArgs e)
        {
            Visible = false;
        }
    }
}
